﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Threading;
using MyFeer.Lib.common;
using MyFeer.Widget.Grid.Editor;
using MyFeer.Widget.Grid.Cell;

namespace MyFeer.Widget.Grid
{
    public class CellEditEventListener
    {
        private GridViewer gridViewer;
        private DispatcherTimer doubleClickTimer;
        private ICellEditor cellEditor;
        private IntegerRect editorRect;
        public CellEditEventListener(GridViewer gridViewer)
        {
            this.gridViewer = gridViewer;
            doubleClickTimer = new DispatcherTimer() { Interval = new TimeSpan(0, 0, 0, 0, 400) };
            doubleClickTimer.Tick += new EventHandler(doubleClickTimer_Tick);
            doubleClickTimer.Start();

        }

        void doubleClickTimer_Tick(object sender, EventArgs e)
        {
            doubleClickTimer.Stop();
        }


        internal void GridViewer_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            if (doubleClickTimer.IsEnabled)
            {
                    // 双击了
                doubleClickTimer.Stop();
                editorRect = this.gridViewer.SelectRect;
                
                IntegerRect cellRect = this.gridViewer.getCellArea(editorRect.Top, editorRect.Left);
                CellModel cellModel = this.gridViewer.GridModel.getCellModel(editorRect.Top, editorRect.Left);
                cellEditor = new TextCellEditor();
                Control editor=cellEditor.createControl();
                editor.MouseLeftButtonDown += new MouseButtonEventHandler(editor_MouseLeftButtonDown);
                editor.LostFocus += new RoutedEventHandler(editor_LostFocus);
                editor.Width = cellRect.Width-2;
                editor.Height = cellRect.Height-2;
               
                Canvas.SetLeft(editor, cellRect.Left+1);
                Canvas.SetTop(editor, cellRect.Top+1);
                Canvas.SetZIndex(editor, 1);

                cellEditor.SetValue(cellModel.CellContent.EditString);
                this.gridViewer.GridCanvas.Children.Add(editor);
            }
            else
            {
                 // 没双击
                doubleClickTimer.Start();
                if(this.cellEditor!=null)
                {
                    this.LostEditorFocus();
                    this.cellEditor = null;
                }
                
            }

        }

        void editor_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            
        }

        void editor_LostFocus(object sender, RoutedEventArgs e)
        {
            this.LostEditorFocus();
        }
        private void LostEditorFocus()
        {
            Control editor = this.cellEditor.getControl();
            editor.LostFocus -= new RoutedEventHandler(editor_LostFocus);

            
            CellModel cellModel = this.gridViewer.GridModel.getCellModel(editorRect.Top, editorRect.Left);
            cellModel.CellContent.EditString = this.cellEditor.GetValue();
            this.gridViewer.GridModel.setCellModel(editorRect.Top, editorRect.Left, cellModel);
            CellItem cellItem = this.gridViewer.GetCellItem(editorRect.Top, editorRect.Left);
            cellItem.SetEditorString(this.cellEditor.GetValue());

            this.gridViewer.GridCanvas.Children.Remove(editor);
        }
    }
}